package com.dwjm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dwjm.entity.Banner;
import com.dwjm.entity.BbsExample;
import com.dwjm.entity.News;
import com.dwjm.service.BannerService;
import com.dwjm.service.BbsExampleService;
import com.dwjm.service.NewsService;

/**
 * 文案表Controller层
 * 
 * @date 2016年12月24日
 * @auther luans
 */
@Controller
@RequestMapping("newsctr")
public class NewsController {

	@Resource
	private NewsService newsService;
	@Resource
	private BbsExampleService bbsExampleService;
	@Resource
	private BannerService bannerService;

	/**
	 * 去文案列表页面：分页查询
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月23日
	 * @auther luans
	 */
	@RequestMapping("newslist")
	public ModelAndView queryListByPage(News news) {
		ModelAndView mv = new ModelAndView();
		if (news.getPageInfo().getPageSize() < 10) {// 每页显示条数
			news.getPageInfo().setPageSize(10);
		}
		List<News> list = newsService.queryListByPage(news);
		if (news.getNewsType() == 4) {// 理事动态
			mv.setViewName("/manager/bbs/bbsDynamic");

		}else if(news.getNewsType() == 5){//学术成果
			mv.setViewName("/manager/academic/academicList");
		} else {
			mv.setViewName("/manager/news/newsList");

		}

		mv.addObject("list", list);
		mv.addObject("news", news);

		return mv;
	}

	/**
	 * 去文案操作页面：添加、编辑、查看
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月24日
	 * @auther luans
	 */
	@RequestMapping("tonewsmange")
	public ModelAndView tonewsManage(HttpServletRequest request,News news) {
		ModelAndView mv = new ModelAndView();
		if (news.getMarkManage() == 1) {
			if(news.getNewsType()==4){
				mv.setViewName("/manager/bbs/bbsDynamicManage");
			}else if(news.getNewsType()==5){
				mv.setViewName("/manager/academic/academicManage");
			}else{
				mv.setViewName("/manager/news/newsManage");
			}

			mv.addObject("news", news);
		} else if (news.getMarkManage() == 2) {

			News n = newsService.selectByPrimaryKey(news.getNewsId());
			n.setCondition(news.getCondition());
			n.setMarkManage(news.getMarkManage());
			n.setPageInfo(news.getPageInfo());
			n=this.setDatetoString(n);
			mv.addObject("news", n);
			
			if(news.getNewsType()==4){
				mv.setViewName("/manager/bbs/bbsDynamicManage");
			}else if(news.getNewsType()==5){
				mv.setViewName("/manager/academic/academicManage");
			}else{
				mv.setViewName("/manager/news/newsManage");

			}
			return mv;

		} else if (news.getMarkManage() == 3) {

			News n = newsService.selectByPrimaryKey(news.getNewsId());
			n.setCondition(news.getCondition());
			n.setMarkManage(news.getMarkManage());
			n.setPageInfo(news.getPageInfo());
			n=this.setDatetoString(n);
			mv.addObject("news", n);
			if(news.getNewsType()==4){
				mv.setViewName("/manager/bbs/bbsDynamicSel");
			}else if(news.getNewsType()==5){
				mv.setViewName("/manager/academic/academicSel");
			}else{
				mv.setViewName("/manager/news/newsSel");

			}
			request.setAttribute("markcontent", n.getNewsContent());
			return mv;
		}
		

		return mv;

	}

	/**
	 * 文案操作：查看、编辑
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月24日
	 * @auther luans
	 */
	@RequestMapping("newsmange")
	public ModelAndView newsManage(News news) {
		if (news.getMarkManage() == 1) {
			news=this.setStringtoDate(news);
			newsService.insertSelective(news);
		} else if (news.getMarkManage() == 2) {
			news=this.setStringtoDate(news);
			newsService.updateByPrimaryKey(news);
		} else if (news.getMarkManage() == 4) {
			News n = new News();
			n.setNewsId(news.getNewsId());
			n.setNewsStatus(3);// 文案状态：1未发布；2已发布；3删除
			newsService.updateByPrimaryKey(n);
		}

		return queryListByPage(news);
	}

	/**
	 * 设置时间格式：String转Date
	 * 
	 * @param News
	 * @return News
	 * @author luans
	 * @date 2016年12月25日
	 *
	 */
	public News setStringtoDate(News news) {

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// 定时开始时间
		if (news.getNewsPublisTime_sel() != null
				&& !"".equals(news.getNewsPublisTime_sel())) {
			try {
				news.setNewsPublisTime(fmt.parse(news.getNewsPublisTime_sel()));
			} catch (ParseException e) {
				System.out.println("转换有日期格式出错！");
			}
		}
		 

		return news;
	}

	/**
	 * 设置时间格式：Date转String
	 * 
	 * @param News
	 * @return News
	 * @author luans
	 * @date 2016年12月25日
	 *
	 */
	public News setDatetoString(News news) {

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// 定时开始时间
		if (news.getNewsPublisTime() != null) {
			news.setNewsPublisTime_sel(fmt.format(news.getNewsPublisTime()));
		}
		 
		return news;
	}
	
	/**
	 * PC端：首页
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月27日
	 * @auther luans
	 */
	@RequestMapping("forum") 
	public ModelAndView forumPage(News news) {
		ModelAndView mv = new ModelAndView();
		//查询banner 
		List<Banner> blist = bannerService.queryListForPC();
		//论坛学人
		List<BbsExample> bbslist=bbsExampleService.queryListForPC();
		//新闻
		List<News> nlist1 = newsService.queryListForPC(1);
		//公告
		List<News> nlist2 = newsService.queryListForPC(3);
		
		
		mv.setViewName("/pc/index");

		mv.addObject("blist", blist);
		mv.addObject("bbslist", bbslist);
		mv.addObject("nlist1", nlist1);
		mv.addObject("nlist2", nlist2);

		return mv;
	}
	
	
	/**
	 * PC端：去文案页列表页面 
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月27日
	 * @auther luans
	 */
	@RequestMapping("pcnewslist")
	public ModelAndView pcqueryListByPage(News news) {
		ModelAndView mv = new ModelAndView();
		news.getPageInfo().setPageSize(8);	
		news.setMarkerpc(1);
		List<News> list = newsService.queryListByPage(news);
		if (news.getNewsType() == 5) {// 学术成果
			mv.setViewName("/pc/academic/academicList");
		} else if (news.getNewsType() == 4) {// 理事动态
			mv.setViewName("/pc/bbs/bbsDynamic");
		} else {
			mv.setViewName("/pc/news/newsList");
		}
		mv.addObject("list", list);
		mv.addObject("news", news);
		
		return mv;
	}
	/**
	 * PC端：去文案页面 
	 * 
	 * @param News
	 * @return ModelAndView
	 * @date 2016年12月28日
	 * @auther luans
	 */
	@RequestMapping("pcnews")
	public ModelAndView pcNews(News news) {
		int id=0;
		if(news!=null && news.getNewsId()!=null){
			id=news.getNewsId();
		}
		System.out.println("id="+id);
		ModelAndView mv = new ModelAndView();
		News news1= newsService.selectByPrimaryKey(id);//
		news1.setMarkerLastNext(1);
		List<News> news2= newsService.selectLastNext(news1);
		news1.setMarkerLastNext(2);
		List<News> news3= newsService.selectLastNext(news1);
		mv.addObject("news", news);
		mv.addObject("news1", news1);
		mv.addObject("news2", news2);
		mv.addObject("news3", news3);
		
		if(news1.getNewsType()==5){
			mv.setViewName("/pc/academic/academicSel");
		}else{
			mv.setViewName("/pc/news/newsSel");
		}
		return mv;
	}

	
}
